package edu.uestc.gene.model;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import edu.uestc.gene.entity.AdverseDrugReaction;
import edu.uestc.gene.entity.DataAnalyzer;
import edu.uestc.gene.entity.Gene;

/**
 * @author Carl
 * 
 *         2013-11-23
 */
public class AnalyzeThread extends Thread {

	private Statistics statistics;
	private AdverseDrugReaction currentADR;
	String PATH = "E:/Saves/gene/anova/";

	public AnalyzeThread(Statistics statistics, AdverseDrugReaction adr) {
		this.statistics = statistics;
		this.currentADR = adr;
	}

	@Override
	public void run() {
		List<Gene> genes = statistics.getGenes();
		Map<String, Integer> gene_encode = statistics.getGenes_encode();
		// for (int i = 0; i < adrs.size(); i++) {
		// AdverseDrugReaction currentADR = adrs.get(i);
		Set<Gene> adr_genes = new HashSet<Gene>();
		for (int j = 0; j < currentADR.getDrugs().size(); j++) {
			adr_genes.addAll(currentADR.getDrugs().get(j).getDrug_genes());
		}
		System.out.println(currentADR.getName() + "可所用于" + adr_genes.size()
				+ "个基因，开始分析。。。");
		// if (adr_genes.size() >= 20) {
		// continue;
		// }
		String[] names = new String[adr_genes.size()];
		double[] codes = new double[adr_genes.size()];
		int index = 0;
		for (Gene gene : adr_genes) {
			names[index] = gene.getName();
			double code = gene_encode.get(gene.getName());
			if (code == 0) {
				code = 0.5;
			}
			codes[index] = code;
			index++;
		}
		StringBuffer sb = new StringBuffer();
		DataAnalyzer dataAnalyzer = DataAnalyzer.getInstance();
		sb.append(currentADR.getName() + "可作用于" + adr_genes.size() + "个基因\n");
		int[][] orthogonalTable = dataAnalyzer.getOrthogonalTable(codes.length);
		// System.out.print(" 正在生成正交表。。。  ");
		// sb.append("正交表:\n");
		// for (int j = 0; j < orthogonalTable.length; j++) {
		// for (int k = 0; k < orthogonalTable.length; k++) {
		// sb.append(orthogonalTable[j][k] + "\t");
		// }
		// sb.append("\n");
		// }

		// System.out.print("正在生成数据表格。。。  ");
		double[][] dataTable = dataAnalyzer.transfer(codes, orthogonalTable);
		for (int j = 1; j < dataTable.length; j++) {
			List<Integer> indexes = new ArrayList<Integer>();
			List<Gene> indexesGene = new ArrayList<Gene>();
			for (int k = 0; k < dataTable[j].length; k++) {
				if (dataTable[j][k] == 2) {
					indexes.add(k);
				}
			}
			for (int k = 0; k < indexes.size(); k++) {
				indexesGene.add(genes.get(k));
			}
			dataTable[j][dataTable[j].length - 1] = statistics
					.getProbByGenesAndADR(indexesGene, currentADR);
		}
		// sb.append("\n用于方差分析的数据表格:\n");
		// for (int j = 0; j < dataTable.length; j++) {
		// for (int k = 0; k < dataTable[j].length; k++) {
		// sb.append(dataTable[j][k] + "\t");
		// }
		// sb.append("\n");
		// }
		// System.out.print("正在进行方差分析。。。");
		String[][] results = dataAnalyzer.getANOVA(codes.length, dataTable);
		sb.append("\n方差分析结果:\n");
		for (int j = 0; j < results.length; j++) {
			for (int k = 0; k < results[j].length; k++) {
				sb.append(results[j][k] + "\t\t\t");
			}
			sb.append("\n");
		}
		Tool.writeText(sb.toString(), PATH + currentADR.getName() + ".txt",
				false);
		System.out.println(currentADR.getName() + "方差分析完成，结果写入文件。");
		// }
	}
}
